package com.huajx.jrb.controller;

import com.huajx.core.annotation.Route;
import com.huajx.core.handler.Controller;
import com.huajx.core.common.Columns;
import com.huajx.jrb.model.User;
import com.huajx.jrb.service.UserService;
import com.huajx.util.StrUtil;

@Route(controllerKey = "/user")
public class UserController extends Controller {

    public void index() {
        int ps = getParaToInt("ps", 10);
        int pi = getParaToInt("pi", 0);

        String account = getPara("account");
        String name = getPara("name");

        Columns columns = new Columns();

        if (StrUtil.isNotBlank(account)) {
            columns.like("account", account);
        }

        if (StrUtil.isNotBlank(name)) {
            columns.like("name", name);
        }

        renderJson(UserService.service.paginate(columns, pi, ps));
    }

    public void edit() {

    }

    public void delete() {
        UserService.service.delete(getParaToLong("id"));
        renderJsonSuccess();
    }

    public void save() {
        User user = getModel(User.class);

        boolean isAdd = user.getId() == null || user.getId().equals(0L);
        if (isAdd) {
            UserService.service.save(user);
        } else {
            UserService.service.update(user);
        }
        renderJsonSuccess();
    }
}
